package com.qf.ssm.controller;


import com.qf.ssm.ex.ControllerException;
import com.qf.ssm.qo.OrderStatusQO;
import com.qf.ssm.qo.PickUpInformationQo;
import com.qf.ssm.result.ResponseResult;
import com.qf.ssm.result.ResultStatus;
import com.qf.ssm.service.CarOrderService;
import com.qf.ssm.service.PickUpInformationService;
import com.qf.ssm.utlis.FilesUtils;
import com.qf.ssm.vo.PickUpInformationVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * @author 黄梓濠
 * @Create 2024-01-04-17:27
 */
@RestController
@RequestMapping("/pickupinfor")
@Api(tags = "取车模块")
public class PickUpInformationController {
    @Autowired
   private PickUpInformationService pickUpInformationService;

    /**
     * 获取取车订单原有的信息
     * @param orderId 订单编号
     * @return
     */
    //展示取车时原有的数据
    @PostMapping("/get/pickupinfor")
    @ApiOperation("获取取车订单原有的数据")
    public ResponseResult<PickUpInformationVo> getPickUpInfor(@RequestParam("orderId") Integer orderId){
        PickUpInformationVo pickUpInformationVo = pickUpInformationService.getPickUpInfor(orderId);
        return ResponseResult.success(pickUpInformationVo);
    }

    /**
     *
     * @param request
     * @param files 文件数组
     * @param orderId 订单编号 id不能重复
     * @return
     */

    //获取取车后面添加的图片
    @PostMapping("/upload/pictures")
    @ApiOperation("取车订单的添加并将订单状态改为已完成")
    public ResponseResult<Integer> uploadPictures(HttpServletRequest request, MultipartFile[] files,@RequestParam("orderId") Integer orderId){
        //图片上传
        String filenames = null;
        try {
            filenames = FilesUtils.fileUpload(request, files);
        } catch (IOException e) {
            throw new ControllerException(ResultStatus.SYS_ERROR);
        }
        PickUpInformationQo pickUpInformationQo = new PickUpInformationQo();
        pickUpInformationQo.setCarPhoto(filenames);
        pickUpInformationQo.setOrderId(orderId);
        System.out.println(pickUpInformationQo);
        Integer count = pickUpInformationService.pickUpInformationFinsh(pickUpInformationQo); //0
        return ResponseResult.success(count);
    }
    //获取取车订单后面添加的数据

    /**
     *
     * @param pickUpInformationQo 里面的orderId为必要参数
     * @return
     */
    @PostMapping("/finish")
    @ApiOperation("取车时添加的图片")
    public ResponseResult<Integer> PickUpInformationFinish(@RequestBody  PickUpInformationQo pickUpInformationQo ){

        if (ObjectUtils.isEmpty(pickUpInformationQo)){
            throw  new ControllerException(ResultStatus.SYS_ERROR);
        }
        Integer count = pickUpInformationService.pickUpInformationFinsh(pickUpInformationQo);

        return ResponseResult.success(count);
    }

}
